package com.workflow.sdk.domain.service;

import com.alibaba.fastjson.JSON;
import com.workflow.sdk.core.WorkflowExecutorResult;
import com.workflow.sdk.domain.model.entity.WorkflowExecuteEntity;
import com.workflow.sdk.domain.model.entity.dto.WorkflowerExecuteParamDto;
import com.workflow.sdk.domain.factories.WorkFlowDomainFactory;
import com.workflow.sdk.domain.model.idenfifer.WorkflowId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Workflower
 *
 * @description: 执行器
 * @author: cyj
 * @date 2022 -06-06 09:52:22
 */
@Service
@Slf4j
public class WorkflowImpl implements Workflow {

    @Autowired
    private Workflow workflow;

    /**
     * Create workflow long 创建一个流程
     *
     * @param workflowId workflow id
     * @param relevance  relevance 自己的关联id
     * @return the long
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createWorkflowExecute(WorkflowId workflowId, String relevance) {

        log.info("createWorkflowExecute: 开始创建流程!, 参数：workflowId[{}], relevance[{}]", workflowId, relevance);

        WorkflowExecuteEntity workflowExecuteEntity = WorkFlowDomainFactory.obtain(workflowId.getId(), relevance);

        // 创建流程
        workflowExecuteEntity.createWorkflowExecute();

        log.info("createWorkflowExecute: 创建流程成功!");

        return workflowExecuteEntity.getIdentify().getExecuteId();
    }

    @Override
    public Long createWorkflowExecuteWithTable(WorkflowId workflowId, String relevance, String tableName) {
        try {
            ReplaceTableInterceptor.setTargetTableName(tableName);
            return workflow.createWorkflowExecute(workflowId, relevance);
        } catch (Exception e) {
            throw e;
        } finally {
            ReplaceTableInterceptor.removeTargetTableName();
        }
    }

    /**
     * Workflow execute 执行工作流程
     *
     * @param <T>                       parameter
     * @param workflowerExecuteParamDto workflow execute id 流程id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public <T> WorkflowExecutorResult workflowExecute(WorkflowerExecuteParamDto<T> workflowerExecuteParamDto) {

        log.info("workflowExecute: 开始执行流程, 参数:{}! ", JSON.toJSONString(workflowerExecuteParamDto));

        WorkflowExecuteEntity workflowExecuteEntity = WorkFlowDomainFactory.obtain(workflowerExecuteParamDto.getWorkflowExecuteId());

        // 执行
        WorkflowExecutorResult workflowExecutorResult = workflowExecuteEntity.workflowExecute(workflowerExecuteParamDto);

        log.info("workflowExecute: 执行流程成功! ");

        return workflowExecutorResult;
    }

    @Override
    public <T> WorkflowExecutorResult workflowExecuteWithTable(WorkflowerExecuteParamDto<T> workflowerExecuteParamDto, String tableName) {
        try {
            ReplaceTableInterceptor.setTargetTableName(tableName);
            return workflow.workflowExecute(workflowerExecuteParamDto);
        } catch (Exception e) {
            throw e;
        } finally {
            ReplaceTableInterceptor.removeTargetTableName();
        }
    }


}
